#include"head.h"
bool login(int socket_fd,client_status *user){

    while(1){
        printf("login: ");
        fflush(stdout);
        char name[10];
        bzero(name,10);
        int read_name= read(STDIN_FILENO,name,10);
        // printf("%c\n",name[0]);
        if(read_name==0){
            printf("断开连接\n");
        }
        if(name[0]>='a'&&name[0]<='z'){
            //登录 输入用户名
            //写一个 限制用户名是小写开头才可以 ---TODO
            //除了第一个后面就不管了 默认后续规范
            strcpy(user->user_name,name);
            user->user_len= read_name;
            user->islogin=1;//登录了

            strcpy(user->path,"");
            int user_send= send(socket_fd,user,sizeof(user),0);
            ERROR_CHECK(user_send,-1,"user_send"); 
            printf("%s登录成功\n",user->user_name);
            return 1;
        }else{
            printf("请输入第一个为小写字母的用户名\n");
        }
    }
    return 0;
}


/*第二版*/
/*
bool login(int socket_fd,client_status *user){
    recvMsg Msg;
    printf("-------------登录--------------");
    while(1){
        printf("请输入用户id: ");
        fflush(stdin);
        char name_id[100];
        bzero(name_id,100);
        int read_name= read(STDIN_FILENO,name_id,10);
        int len=strlen(name_id);
        name_id[len]='\0';
        // printf("%c\n",name[0]);
        int index=0;
        int flag=0;
        while(1){
            if(index==len){
                printf("index:%d len:%d\n",index,len);
                break;
            }
            if(name_id[index]>='0'&&name_id[index]<='9'){
                index++;
                continue;
            }
            else{
                flag=1;
                break;
            }

        }
        if(flag==0){
            printf("请输入您的密码:");
            fflush(stdin);
            int ret_read=read(STDIN_FILENO,&user->password,sizeof(user->password));
            ERROR_CHECK(ret_read,-1,"ret_read");
            len = strlen(user->password);
            user->password[len]='\0';
            printf("password :%s\n",user->password);

            strcpy(user->user_name,name_id);
            user->user_len= read_name;
            user->islogin=1;//登录了
            strcpy(user->path,"");

            int user_send= send(socket_fd,user,sizeof(user),0);
            if(user_send==-1){
               // LOG(user,msg);
            }
            int user_recv=recv(socket_fd,&Msg,sizeof(Msg),0);
            if(user_recv==-1){

            }
            if(strcpy(Msg.statu,"1")==0){
                printf("%s登录成功\n",user->user_name);
                return 1;
            }else{
                printf("用户名或密码错误\n");
                continue;
            }
        }
        
        else{
            printf("请输入由数字组成成的用户名\n");
            flag=0;
            continue;
        }
    }

    return 0;
}*/



bool unlogin(int socket_fd,client_status *user){
    recvMsg Msg;
    user->islogin=0;
    sendMsg msg;
    msg.mode=-1;
    int ret_send=send(socket_fd,&msg,sizeof(msg),0);
    ERROR_CHECK(ret_send,-1,"ret_send");
    int ret_recv= recv(socket_fd,&Msg,sizeof(Msg),0);
    ERROR_CHECK(ret_recv,-1,"ret_recv");

    if(strcmp(Msg.statu,"1")==0){
        return 1;    
    }
    return 0;
}

